package com.aisino;

import cn.hutool.core.lang.Console;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.hutool.log.dialect.commons.ApacheCommonsLogFactory;
import cn.hutool.log.dialect.console.ConsoleLogFactory;
import cn.hutool.log.dialect.jdk.JdkLogFactory;
import cn.hutool.log.level.Level;
import cn.hutool.setting.dialect.Props;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author guochuantao
 * @version 1.0
 * @description
 * @since 2024/7/3 15:30
 */
public class HutoolLearn {
    public static void main(String[] args) {
        // 读取xml
/*        Document document = XmlUtil.readXML(new File("src/main/resources/test.xml"));
        Element element = XmlUtil.getElementByXPath("//returnsms/returnstatus", document);
        String code = element.getAttribute("code");
        System.out.println("code = " + code);
        Object byXPath = XmlUtil.getByXPath("//returnsms/message", document, XPathConstants.STRING);
        System.out.println("byXPath = " + byXPath);*/


        String parametersJson = "[{\"name\":\"name\",\"value\":\"张三\"},{\"name\":\"age\",\"value\":\"14\"}]";
        HashMap<String, String> requestMap = new HashMap<>(4);
        requestMap.put("name", "张三11");
        requestMap.put("age", "19");
        requestMap.put("sex1", "男");
        //转换json为list
        List<JSONObject> params = JSON.parseArray(parametersJson, JSONObject.class);
        if (params != null) {
            ArrayList<HashMap<String, String>> list = new ArrayList<>();
            //校验请求参数中的参数 如果和数据库匹配上的以新传入的动态值为准
            List<JSONObject> finalParams = params;
            requestMap.forEach((k, v) -> {
                finalParams.forEach(param -> {
                    if (param.get("name").equals(k)) {
                        HashMap<String, String> finalMap = new HashMap<>(4);
                        finalMap.put("name", k);
                        finalMap.put("value", v);
                        list.add(finalMap);
                    }
                });
            });
            params = JSONObject.parseArray(JSON.toJSONString(list), JSONObject.class);
            System.out.println("params = " + params);
        }
        
        Props props = new Props("db.properties");
        Console.log(props.get("username"));

        Log log = LogFactory.get();

        log.debug("This is {} log", Level.DEBUG);
        log.info("This is {} log", Level.INFO);
        log.warn("This is {} log", Level.WARN);

        Exception e = new Exception("test Exception");
        log.error(e, "This is {} log", Level.ERROR);


        log.info("==============================================");
        String result = HttpUtil.get("localhost:8081/apiservice/apiThirdPartyConfig/selectOne?id=1805877855783505921");
        Console.log(result);

        log.info("==============================================");
        String body = HttpRequest.get("localhost:8081/apiservice/apiThirdPartyConfig/selectOne?id=1805877855783505921")
                .auth("eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6Ijk5MGI2ZjNmLWRkNDUtNDNlNC05NzVmLTI4NTBmYTZkOTNlNyJ9.3sIJe1oaAViW3_WyHaHr0RcA-aihuNV9eeHE1qzCM-3a3oPg0MFnECXBrsFzqJdCKmOx1VPwukLWMkR5YXt5kA")
                .execute()
                .body();

        Console.log(JSONUtil.toJsonPrettyStr(body));


    }
}
